package com.sale.succ.forms;

import com.sale.succ.components.DefButton;
import com.sale.succ.components.DefForm;
import com.sale.succ.components.DefInput;
import com.sale.succ.components.DefVerScrollPanel;
import com.sale.succ.datacomp.trade.TradeTable;
import com.sale.succ.utils.TraderConstants;
import com.sale.succ.utils.TraderHelper;

import eve.fx.Insets;
import eve.fx.gui.WindowConstants;
import eve.ui.CellPanel;

public class TradeForm extends DefForm {
	private TradeTable tradeTable;
	private CellPanel mainPanel;
	private DefInput searchField;
	private DefButton searchButton;
	private CellPanel topPanel;

	public TradeForm() {
		title = TraderHelper.getLabel(TraderConstants.TRADE);
		addLast(getMainPanel());
		doButtons(CANCELB);
		windowFlagsToSet |= WindowConstants.FLAG_MAXIMIZE;
	}

	private CellPanel getMainPanel() {
		if (mainPanel == null) {
			mainPanel = new CellPanel();
			// mainPanel.addLast(new
			// DefLabel(TraderHelper.getLabel(TraderConstants.SELL_TRADE))).setControl(HCENTER).setCell(HGROW);
			// mainPanel.addLast(getTopPanel()).setControl(HCENTER).setCell(HGROW);
			DefVerScrollPanel scrollPanel = new DefVerScrollPanel(
					getTradeTable());
			mainPanel.addLast(scrollPanel);

		}
		return mainPanel;
	}

	private CellPanel getTopPanel() {
		if (topPanel == null) {
			topPanel = new CellPanel();
			topPanel.defaultTags.set(TAG_INSETS, new Insets(0, 0, 0, 0));
			topPanel.addNext(getSearchField());
			topPanel.addNext(getSearchButton());

		}
		return topPanel;
	}

	private DefInput getSearchField() {
		if (searchField == null) {
			searchField = new DefInput();
			searchField.setPreferredSize(50, 20);
		}
		return searchField;
	}

	private DefButton getSearchButton() {
		if (searchButton == null) {
			searchButton = new DefButton(TraderHelper
					.getLabel(TraderConstants.SEARCH));
			searchButton.action = TraderConstants.SEARCH_ACTION;
			searchButton.setPreferredSize(50, 20);
		}
		return searchButton;
	}

	private TradeTable getTradeTable() {
		if (tradeTable == null) {
			tradeTable = new TradeTable();
			tradeTable.setPreferredSize(40, tradeTable.getPrefHeight());

		}
		return tradeTable;
	}

	public boolean handleAction(String action) {

		/*
		 * if (action.equals(TraderConstants.SEARCH_ACTION)) {
		 * getTradeTable().search(getSearchField().getText()); return true; }
		 */

		return super.handleAction(action);
	}

}
